home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7663 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  89 lines

  1. Path: titan.fullerton.edu!grosin
  2. From: grosin@titan.fullerton.edu (Gil Rosin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help a newbie with 2 simple questions...
  5. Date: 24 Feb 1996 22:24:36 GMT
  6. Organization: California State University at Fullerton
  7. Message-ID: <4go374$619@wintermute.ecs.fullerton.edu>
  8. NNTP-Posting-Host: titan.ecs.fullerton.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hi, I've just started playing around with C++ so these are basic questions. I'm
  12. using Borland C++ v3.1, so this might not be applicable to the c++ general
  13. group, but I am not too familiar with C++, so I'll ask anyways :)
  14.  
  15. 1) is there any formatting available with cout? other then cout.width()? A book
  16.    I have says it is acceptable to mix a ANSI C and C++ for stuff like sprintf
  17.    and the like to do formatted output??
  18.  
  19. 2) I've begun to play around with reference variables and reference functions.
  20.    What are REAL world uses for it? I've come up with the following:
  21.    a) If I pass an integer to a function and want to be able to modify it, I
  22.       have to use pointers, that means asterisks and ambersands all over the
  23.       place, but with referecing, I only need one ambersand, it doesn't seem
  24.       like a must, just a convience.
  25.    b) reference functions
  26.  
  27. Ok, make it 3, question on reference functions, what can I do with them? An
  28. example in the book shows something like this:
  29.  
  30. int Array[10];
  31.  
  32. int &Test(int Index);
  33.  
  34. main()
  35.  {
  36.   Test(2) = 45;
  37.   return 0;
  38.  }
  39.  
  40. int &Test(int Index)
  41.  {
  42.   return Array[Index];
  43.  }
  44.  
  45. in this example, Array[2] is assigned 45, all nice and cool, but is this all
  46. I can do with it?
  47.  
  48. I tried to do my own example with a simple struct:
  49.  
  50. typedef struct
  51.  {
  52.   char Name[40];
  53.   int ID;
  54.  } Student;
  55.  
  56. Student Std[20];
  57.  
  58. Student &Student(int Index);
  59.  
  60. main()
  61.  {
  62.   sprintf(Std[1].Name, "Test");
  63.   Std[1].ID = 20;
  64.   
  65.   Student(10) = Std[1];
  66.   return 0;
  67.  }
  68.  
  69. Student &StudentInit(int Index)
  70.  {
  71.   return Std[Index];
  72.  }
  73.  
  74. Sorry, had a few typos in there, obvious the "Student" function has too be
  75. called something else, but anyways, the only use I could find for something
  76. like this is to copy one record to another position. Is this all I could
  77. do with it? I thought perhaps I could say something like:
  78.  
  79. StudentInit(20) = "Test", 19;
  80.  
  81. or something like that to initiaize the record, the above line probably doesn't
  82. make sense now that I Think about it, but just something of that basic idea.
  83.  
  84. Please reply via email to grosin@titan.fullerton.edu
  85.  
  86. Thanks.
  87.  
  88.  
  89.